package org.lex.io.filesystem;

import java.io.File;

/**
 * �����ļ�·��/�ļ���ķ���
 * 
 * e.g.: c:\foo\bar.txt /root/foo/bar.txt file name : bar.txt
 * ���һ���ָ���('/'����'\'��':') ֮��������ַ� base name : bar
 * �ļ��������һ��'.'֮ǰ�������ַ�(����Ϊ"",e.g: ".hiden") extension : txt
 * �ļ��������һ��'.'֮��������ַ�(����Ϊ"") parent : c:\foo /root/foo
 * ���һ���ļ��ָ���֮ǰ�������ַ�,�������ָ���
 */

public class FileNameUtilities {
	private static char defaultSeparatorChar = File.separatorChar;

	public static String getFullPath(String path, String file) {
		return getFullPath(path, file, defaultSeparatorChar);
	}

	public static String getFullPath(String path, String file, char separatorChar) {
		StringBuilder buffer = new StringBuilder();
		buffer.append(getStandardPath(path, separatorChar));
		buffer.append(separatorChar);
		if (file.length() > 0 && separatorChar == file.charAt(0))
			buffer.append(file, 1, file.length());
		else
			buffer.append(file);
		return buffer.toString();
	}

	/*
	 * e.g.: c:\foo\bar\ or /root/foo/bar/ IS NOT standard but: c:\foo\bar or
	 * /root/foo/bar IS standard
	 */
	public static String getStandardPath(String path, char separatorChar) {
		if (true == isStandardPath(path, separatorChar))
			return path;
		else
			return path.substring(0, path.length() - 1);
	}

	public static String getStandardPath(String path) {
		return getStandardPath(path, defaultSeparatorChar);
	}

	public static boolean isStandardPath(String path, char separatorChar) {
		if (0 == path.length())
			return true;
		return separatorChar != path.charAt(path.length() - 1);
	}

	public static String getFileName(String path) {
		return getFileName(path, File.separatorChar);
	}

	/**
	 * ����·��<code>path</code>�У����һ���ļ��ָ���֮����ַ�
	 * 
	 * @param path
	 * @param separatorChar
	 * @return
	 */
	public static String getFileName(String path, char separatorChar) {
		if (null == path || 0 == path.length())
			return "";
		path = getStandardPath(path, separatorChar);
		int index = path.lastIndexOf(separatorChar);
		if (-1 == index)
			return path;
		else
			// c:\foo\bar.txt --> bar.txt
			return path.substring(index + 1);
	}

	public static String getBaseName(String path) {
		return getBaseName(path, File.separatorChar);
	}

	public static String getBaseName(String path, char separatorChar) {
		String name = getFileName(path, separatorChar);
		int length = name.length();
		if (0 == length)
			return "";
		// e.g.: i.love.china.txt --> i.love.china
		int index = name.lastIndexOf('.');
		if (0 == index)
			// unix file system : .bashrc
			return "";
		else if (-1 == index)
			// some file don't has an extension name
			return name;
		else
			return name.substring(0, index);
	}

	public static String getExtension(String name) {
		return getExtension(name, File.separatorChar);
	}

	public static String getExtension(String name, char separatorChar) {
		name = getStandardPath(name, separatorChar);
		int length = name.length();
		if (0 == length)
			return "";
		// e.g.: i.love.china.txt --> txt
		int index = name.lastIndexOf('.');
		if (-1 == index || name.length() - 1 == index)
			// some file don't has an extension name
			// like "GUIDE", "tmp.", etc.
			return "";
		else
			return name.substring(index + 1);
	}

	public static String getParentPath(String path) {
		return getParentPath(path, File.separatorChar);
	}

	public static String getParentPath(String path, char separatorChar) {
		if (null == path || 0 == path.length())
			return "";
		path = getStandardPath(path);
		int index = path.lastIndexOf(separatorChar);
		if (-1 == index)
			return "";
		if (0 == index)
			index = 1; // like "/root"
		return path.substring(0, index);
	}
}
